/*
 * @Author: master
 * @Date: 2021-12-02 13:53:52
 * @LastEditTime: 2022-04-13 15:52:50
 * @Description: 地图工具服务
 */
/**
 * 路况查询框选范围坐标转化
 */
export const coordinatesHandleToTraffic = list => {
  if (list.length === 0) {
    return '';
  }
  // 取出二维坐标点
  const coordinates = list.map(item => item.slice(0, 2));
  const resultCoordinates = [];
  // 根绝对角线点算出另外两个点
  resultCoordinates[0] = coordinates[0];
  resultCoordinates[1] = [coordinates[1][0], coordinates[0][1]];
  resultCoordinates[2] = coordinates[1];
  resultCoordinates[3] = [coordinates[0][0], coordinates[1][1]];
  resultCoordinates[4] = coordinates[0];
  // 将坐标点转换为查询参数
  return resultCoordinates.map(item => item.join(' ')).toString();
};
/**
 * 修改坐标串为一维数组(中国范围内)
 */
export const changeOneArray = (strings, separator = ',') => {
  const [arr, array] = [strings.split(separator), []];
  if (arr.length > 1) {
    for (let i = 0, lens = arr.length; i < lens; i++) {
      const [x, y] = [Number(arr[i]), Number(arr[i + 1])];
      if (y) {
        if (isNaN(x) || isNaN(y) || y <= 3.85 || y >= 53.55 || x <= 73.55 || x >= 135.083) {
          i = i + 1;
          continue;
        }
        array.push(x);
        array.push(y);
      }
      i = i + 1;
    }
  }
  return array;
};
/**
 * 修改坐标串为二维数组(中国范围内)
 */
export const changeTwoArray = (strings, separator = ',') => {
  const [arr, array] = [strings.split(separator), []];
  if (arr.length > 1) {
    for (let i = 0, lens = arr.length; i < lens; i++) {
      let [arrChild, x, y] = [[], Number(arr[i]), Number(arr[i + 1])];
      if (y) {
        if (isNaN(x) || isNaN(y) || y <= 3.85 || y >= 53.55 || x <= 73.55 || x >= 135.083) {
          i = i + 1;
          continue;
        }
        arrChild = [x, y];
      }
      if (arrChild.length) array.push(arrChild);
      i = i + 1;
    }
  }
  return array;
};
/**
 * 数组坐标处理（返回array）
 */
export const arrayCoordinatesHandle = list => {
  const array = [];
  if (list.length > 0) {
    for (let i = 0, lens = list.length; i < lens; i++) {
      const arr = [list[i][0], list[i][1]];
      array.push(arr);
    }
  }
  return array;
};
/**
 * 数组坐标处理（返回string）
 */
export const arrayCoordinatesHandleToSting = list => {
  let arrayString = '';
  if (list.length > 0) {
    for (let i = 0, lens = list.length; i < lens; i++) {
      const arr = [list[i][0], list[i][1]];
      arrayString += i === 0 ? arr.toString() : ',' + arr.toString();
    }
  }
  return arrayString;
};
/**
 * @description: 转换大成返回的路况轨迹坐标
 * @param {Array} data ===> 大成发挥的地图坐标数据
 * @return: Array 转换后的路径坐标
 */
export const handleTranslatePointForDC = data => {
  const result = [];
  for (let i = 0, len = data.length; i < len; i++) {
    for (let n = 0, plen = data[i].points.length; n < plen; n++) {
      result.push(data[i].points[n].point);
    }
  }
  return result;
};
/**
 * 图形定位(主要针对多个点或单个点定位，注：必须要是包含[item.x,item.y]属性的数组类型)
 */
export const graphicPositioning = (data, MapUtils) => {
  // 无数据
  if (!data) return;
  // 传入的数据非数组
  const list = data instanceof Array ? data : [data];
  // 只有一个点
  if (list.length === 1) {
    const [{ x, y }] = list;
    // 定位
    MapUtils.setMapLocation({ center: [x, y] });
    return;
  }
  // 定位面
  const coordinates = list.map(item => [+item.x, +item.y]).filter(item => item.every(cItem => !isNaN(cItem)));
  const geoJson = {
    type: 'Feature',
    properties: {},
    geometry: {
      type: 'LineString',
      coordinates: coordinates,
    },
  };
  MapUtils.locateByGraphic(geoJson);
};
/**
 * 图形定位(主要针对多个点或单个点定位，注：必须要是包含[x,y]属性的数组类型)
 */
export const grapPosBycoords = (list, MapUtils) => {
  if (list.length) {
    let geoJson = '';
    // 线定位
    if (list.length > 1) {
      const coordinates = [...list];
      geoJson = { type: 'Feature', geometry: { type: 'LineString', coordinates: coordinates }, properties: {} };
      MapUtils.locateByGraphic(geoJson);
    } else {
      // 点定位
      MapUtils.setMapLocation({ center: list }); // 定位
    }
  }
};
/**
 * 处理边界坐标（多个飞地问题）
 */
export const handleMoreStrToArray = geoStr => {
  let borderArray = [];
  geoStr = geoStr.replace(/;/g, '|');
  if (geoStr.indexOf('|') >= 0) {
    borderArray = geoStr.split('|');
  } else {
    borderArray.push(geoStr);
  }
  return borderArray;
};
/**
 * 处理边界坐标（多个飞地问题）取单一坐标点
 */
export const handleSingleMoreStrToArray = geoStr => {
  let bounds = '';
  geoStr = geoStr.replace(/;/g, '|');
  if (geoStr.indexOf('|') >= 0) {
    bounds = geoStr.split('|')[0];
  } else {
    bounds = geoStr;
  }
  return bounds;
};
/**
 * 获取抽稀距离倍数
 */
export const getSimParams = level => {
  level = parseInt(level);
  const simParams = {
    1: { kind: '00,01', sim: 75000 },
    2: { kind: '00,01', sim: 65000 },
    3: { kind: '00,01', sim: 55000 },
    4: { kind: '00,01', sim: 45000 },
    5: { kind: '00,01', sim: 35000 },
    6: { kind: '00,01', sim: 20000 },
    7: { kind: '00,01', sim: 20000 },
    8: { kind: '00,01', sim: 15000 },
    9: { kind: '00,01', sim: 5000 },
    10: { kind: '00,01', sim: 2000 },
    11: { kind: '00,01,02,03,04', sim: 1500 },
    12: { kind: '00,01,02,03,04', sim: 0 },
    13: { kind: '00,01,02,03,04', sim: 0 },
    14: { kind: '00,01,02,03,04,06', sim: 0 },
    15: { kind: '00,01,02,03,04,06', sim: 0 },
    16: { kind: '00,01,02,03,04,06', sim: 0 },
    17: { kind: '00,01,02,03,04,06', sim: 0 },
    18: { kind: '00,01,02,03,04,06', sim: 0 },
    19: { kind: '00,01,02,03,04,06', sim: 0 },
    20: { kind: '00,01,02,03,04,06', sim: 0 },
  };
  if (!simParams[level]) {
    return { kind: '00,01,02,03,04,06', sim: 0 };
  } else {
    return simParams[level];
  }
};
/**
 * 转换多维坐标
 */
export const translateCoordinate = data => {
  // 无数据
  if (!data) return [];
  // 判断是否已经是坐标数据
  if (data.length && !Array.isArray(data[0])) return data;
  // 多维数组坐标转为正常的二维数组坐标
  return data.reduce((total, item) => {
    return item.length && Array.isArray(item[0]) ? total.concat(translateCoordinate(item)) : [...total, item];
  }, []);
};
